package com.kehutong.admin.app;

import com.kehutong.admin.entity.Corp;
import com.kehutong.admin.entity.CorpModuleVersion;
import com.kehutong.admin.service.CorpModuleVersionService;
import com.kehutong.common.DoveClient;
import com.kehutong.common.errcode.Result;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import java.util.List;

/**
 * 企业信息
 * @author: liukunlong
 * @date: 2021-03-29 14:04
 */
@MvcAction
@ReqMapping("/kht-bin/core/corp")
public class CorpAction {
    private static final Logger logger = LoggerFactory.getLogger(CorpAction.class);

    @Inject
    private DoveClient doveClient;

    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private CorpModuleVersionService corpModuleVersionService;

    /**
     * 获取企业的小区信息列表(小区中包含企业信息)
     * @param jsonObject 查询条件
     * @return 企业的小区信息列表
     * @throws Exception 异常
     */
    @ReqMapping("/community/list")
    public Object list(JSONObject jsonObject) throws Exception{
        JSONArray result=new JSONArray();
        List<Corp> corpList=jdbcSession.findArray(Corp.class)
                .eq(jsonObject,"id")
                .exe();
        jsonObject.getString("areaName");
        if(Objects.nonEmpty(corpList)){
            for (Corp corp:corpList){
                JSONObject communityResult=doveClient.post("/basic/community/service/list",(http)->{
                    http.addHeader("companyNo",corp.getId());
                    String areaName=jsonObject.getString("areaName");
                    if(Objects.nonEmpty(areaName)){
                        http.addParam("area_names",jsonObject.getString("areaName"));
                        http.addParam("area_ids",jsonObject.getString("areaId"));
                        http.addParam("name",jsonObject.getString("name"));
                    }
                });
                if(communityResult.getIntValue("code")==200){
                    JSONArray communityList = communityResult.getJSONArray("data");
                    if(Objects.nonEmpty(communityList)){
                        for(Object o:communityList){
                            JSONObject communityJson=(JSONObject)o;
                            communityJson.put("companyNo",corp.getId());
                            communityJson.put("companyName",corp.getName());
                            result.add(communityJson);
                        }
                    }

                }
            }
        }
        return result;
    }

    @ReqMapping("/module/version/get")
    public Object getCorpModuleVersion(String companyNo) throws Exception{
        return corpModuleVersionService.get(companyNo);
    }

    /**
     * 初始化用户资产绑定编号
     * @param jsonObject
     * @return
     */
    @ReqMapping("/initOwnerNo")
    public Object initOwnerNo(JSONObject jsonObject) throws Exception{
        List<Corp> corpList=jdbcSession.findArray(Corp.class)
                .eq(jsonObject,"id")
                .noEq("id","1")
                .eq("deleted", false)
                .exe();
        jsonObject.getString("areaName");
        if(Objects.nonEmpty(corpList)){
            for (Corp corp:corpList){
                CorpModuleVersion corpModuleVersion=jdbcSession.findOne(CorpModuleVersion.class)
                        .eq("corpNo",corp.getId()).exe();
                if(Objects.isNull(corpModuleVersion)){
                    corpModuleVersion= Pool.newInstance(CorpModuleVersion.class);
                    corpModuleVersion.setCorpNo(corp.getId());
                    corpModuleVersion.setPayVersion("1.0");
                    jdbcSession.insert(corpModuleVersion);
                }
                logger.info("----------------------------------------------initOwnerNo:{} begin----------------------------------------------",corp.getName());
                JSONObject result=doveClient.post("/crm/resident/service/initOwnerNo",(http)->{
                    http.addHeader("companyNo",corp.getId());
                });
                logger.info("-构建生成编号,结果：{}",result);
                logger.info("----------------------------------------------initOwnerNo:{} end----------------------------------------------",corp.getName());
            }
        }
        return Result.success();
    }
}
